package com.oneguy.model;

import com.oneguy.Cache;
import com.oneguy.Log;
import com.oneguy.Query;
import com.oneguy.control.util.TextUtils;
import com.oneguy.model.bean.*;

import java.util.List;

/**
 * Created by ZuoShu on 17/1/3.
 */
public class QueryUtil {
    private static final String TAG = "QueryUtil";

    // 捕鱼数据汇总
    public static String getFishDataString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getSzFishFactorString() + "\n");
        sb.append(getFishTodaySumString() + "\n");
        sb.append(getSzFishOnlineSum()+"\n");
        sb.append(getFishSettingUpdateTime());
        return sb.toString();
    }

    // 捕鱼在线数据
    public static String getSzFishOnlineSum(){
        StringBuffer sb = new StringBuffer();
        FishOnlineSum fishOnlineSum = Query.getInstance().queryFishOnlineSum();
        sb.append("=====在线详情=====\n");
        sb.append("上桌:"+fishOnlineSum.getOntable()+"\n");
        sb.append("day1:"+fishOnlineSum.getDay1()+"\n");
        sb.append("day2:"+fishOnlineSum.getDay2()+"\n");
        sb.append("day3:"+fishOnlineSum.getDay3()+"\n");
        sb.append("day4:"+fishOnlineSum.getDay4()+"\n");
        sb.append("day5:"+fishOnlineSum.getDay5()+"\n");
        sb.append("day6:"+fishOnlineSum.getDay6()+"\n");
        sb.append("day7:"+fishOnlineSum.getDay7()+"\n");
        return sb.toString();
    }

    // 捕鱼参数
    public static String getSzFishFactorString() {
        List<RoomInfoEntity> rooms = Cache.getInstance().getRooms();
        if (rooms == null || rooms.size() == 0) {
            return "数据未初始化";
        }
        StringBuffer sb = new StringBuffer();
        for (RoomInfoEntity room : rooms) {
            sb.append("房间:" + room.getRoomGuid() + "=====\n"
                    + "桌子:" + room.getFirstTableGuid() + "-" + room.getEndTableGuid() + "\n"
                    + "参数:" + room.getFactor() + "\n");
        }
        return sb.toString();
    }

    // 捕鱼当日数据
    public static String getFishTodaySumString() {
        StringBuilder sb = new StringBuilder();
        FishTodaySum fishTodaySum = Query.getInstance().queryFishTodaySum();
        sb.append("=====今日详情=====\n");
        sb.append("上桌:" + fishTodaySum.getOntable() + " 赢:" + fishTodaySum.getWin());
        return sb.toString();
    }

    // 捕鱼数据更新时间
    public static String getFishSettingUpdateTime() {
        StringBuilder sb = new StringBuilder();
        int count = (int) ((System.currentTimeMillis()
                - Cache.getInstance().getLastReadRoomTime()) / (60 * 1000));
        sb.append("同步时间：" + count + "分钟前");
        return sb.toString();
    }

    public static String getSzDataString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getSzRegisterCount() + "\n");
        sb.append(getPayScoreString() + "\n");
        sb.append(getRevenueString() + "\n");
        sb.append(getMaxOnlineString() + "\n");
        sb.append(getActiveUserString() + "\n");
        sb.append(getOnlineCountString() + "\n");
        sb.append(getOnlineDetailString() + "\n");
        sb.append(getActiveUserDetailString() + "\n");
        sb.append(getServerTimeString() + "\n");
        return sb.toString();
    }

    public static String getServerTimeString() {
        return "服务器时间:" + TextUtils.simpleFormatTime(System.currentTimeMillis());
    }

    public static String getSzRegisterCount() {
        StringBuilder sb = new StringBuilder();
        List<RegisterCount> registerCountList = Query.getInstance().queryRegisterCount();
        if (registerCountList != null) {
            sb.append("=====注册人数=====\n");
            for (RegisterCount registerCount : registerCountList) {
                sb.append(registerCount.getDate() + " " + registerCount.getCount() + "\n");
            }
        }
        return sb.toString();
    }

    public static String getPayScoreString() {
        StringBuilder sb = new StringBuilder();
        List<PayScore> payScoreList = Query.getInstance().queryPayScore();
        if (payScoreList != null) {
            sb.append("=====充值=====\n");
            for (PayScore payScore : payScoreList) {
                sb.append(payScore.getDate() + " " + payScore.getValue() + "\n");
            }
        }
        return sb.toString();
    }

    public static String getOnlineCountString() {
        return "实时在线:" + Query.getInstance().queryOnlineCount() + "\n";
    }

    // 在线详情
    public static String getOnlineDetailString() {
        StringBuilder sb = new StringBuilder();
        List<OnlineDetail> onlineDetailList = Query.getInstance().queryOnlineDetail();
        if (onlineDetailList != null && onlineDetailList.size() > 0) {
            sb.append("=====在线详情=====\n");
            for (OnlineDetail onlineDetail : onlineDetailList) {
                sb.append(onlineDetail.getName() + " " + onlineDetail.getCount() + "\n");
            }
        }
        return sb.toString();
    }

    // 日活详情
    public static String getActiveUserDetailString() {
        StringBuilder sb = new StringBuilder();
        List<ActiveUserDetail> activeUserDetailList = Query.getInstance().queryActiveUserDetail();
        if (activeUserDetailList != null && activeUserDetailList.size() > 0) {
            sb.append("=====日活详情=====\n");
            for (ActiveUserDetail activeUserDetail : activeUserDetailList) {
                sb.append(activeUserDetail.getName() + " " + activeUserDetail.getCount() + "\n");
            }
        }
        return sb.toString();
    }

    // 抽水合计
    public static String getRevenueString() {
        StringBuilder sb = new StringBuilder();
//        List<RevenueScore> revenueScoreList = Query.getInstance().queryRevenue();
        List<RevenueScore> revenueScoreList = Cache.getInstance().getRevenueScoreList();
        if (revenueScoreList != null && revenueScoreList.size() > 0) {
            sb.append("=====日收入=====\n");
            for (RevenueScore revenueScore : revenueScoreList) {
                sb.append(revenueScore.getDate() + " " + revenueScore.getRevenue() + "\n");
            }
        } else {
            sb.append("=====日收入数据未初始化=====\n");
        }
        return sb.toString();
    }

    // 日活
    public static String getActiveUserString() {
        StringBuilder sb = new StringBuilder();
        List<ActiveUser> activeUserList = Cache.getInstance().getActiveUserList();
        if (activeUserList == null || activeUserList.size() == 0) {
            sb.append("=====日活数据未初始化=====\n");
        } else {
            sb.append("=====日活=====\n");
            for (ActiveUser activeUser : activeUserList) {
                sb.append(activeUser.getDate() + " " + activeUser.getCount() + "\n");
            }
        }
        return sb.toString();
    }

    // 最高在线
    public static String getMaxOnlineString() {
        StringBuilder sb = new StringBuilder();
        List<MaxOnline> maxOnlineList = Query.getInstance().queryMaxOnline();
        if (maxOnlineList != null) {
            sb.append("=====最高在线=====\n");
            for (MaxOnline online : maxOnlineList) {
                sb.append(online.getDate() + " " + online.getCount() + " " + online.getProduceTime() + "\n");
            }
        }
        return sb.toString();
    }

    public static String queryUserDetail(int id) {
        Log.i(TAG, "queryUserDetail:" + id);
        StringBuilder sb = new StringBuilder();
        List<UserTransfer> userTransferList = Query.getInstance().queryUserTransfer(id);
        if (userTransferList != null) {
            sb.append("=====转账记录=====\n");
            sb.append("日期，转入，转出\n");
            for (UserTransfer userTransfer : userTransferList) {
                sb.append(userTransfer.getDate() +
                        "|" + TextUtils.fixLength(userTransfer.getInScore(), 10) +
                        "|" + TextUtils.fixLength(userTransfer.getOutScore(), 10) + "\n");
            }
        }
        return sb.toString();
    }

    public static String queryUserTransferDetail(int id) {
        Log.i(TAG, "queryUserDetail:" + id);
        StringBuilder sb = new StringBuilder();

        List<UserTransferDetail> userTransferInDetailList = Query.getInstance().queryUserTransferInDetail(id);
        if (userTransferInDetailList != null) {
            sb.append("=====转入详情=====\n");
            for (UserTransferDetail detail : userTransferInDetailList) {
                sb.append(detail.getDate() +
                        "|" + detail.getScore() +
                        "|" + detail.getNickName() + "\n");
            }
        }

        List<UserTransferDetail> userTransferDetailList = Query.getInstance().queryUserTransferOutDetail(id);
        if (userTransferDetailList != null) {
            sb.append("=====转出详情=====\n");
            for (UserTransferDetail detail : userTransferDetailList) {
                sb.append(detail.getDate() +
                        "|" + detail.getScore() +
                        "|" + detail.getNickName() + "\n");
            }
        }
        return sb.toString();
    }

}
